package com.zxc.web;

import com.github.pagehelper.PageInfo;
import com.zxc.entity.Dept;
import com.zxc.entity.Emp;
import com.zxc.services.DeptServices;
import com.zxc.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/20 19:37
  * @description:
  */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpServices empServices;
    @Autowired
    private DeptServices deptServices;

    @RequestMapping("/getAll")
    public String getAll(Model model) {
        List<Emp> all=empServices.getAll();
        model.addAttribute("empList",all);
        return "index";
    }

    @RequestMapping("/getByPage")
    public String getAll(Model model,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){

        PageInfo<Emp> all = empServices.getByPage(pageNum);
        model.addAttribute("empList", all);
        return "index";
    }

    @RequestMapping("/addEmp")
    public String addEmp(Emp emp,Model model){
        boolean b = empServices.addEmp(emp);
        String msg = "新增失败";
        if (b) {
            msg = "新增成功";
        }
        model.addAttribute("msg", msg);
        return "message";
    }

    @RequestMapping("/getEmpByEno/{eno}")
    public String getEmpByEno(@PathVariable Integer eno,Model model){
        Emp emp = empServices.getEmpByEno(eno);
        List<Dept> deptList = deptServices.getDeptList();
        model.addAttribute("emp", emp);
        model.addAttribute("deptList", deptList);
        return "updateEmp";
    }

    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp,Model model){
        boolean b = empServices.updateEmp(emp);
        String msg = "修改失败";
        if (b) {
            msg = "修改成功";
        }
        model.addAttribute("msg", msg);
        return "message";
    }
    @RequestMapping("/delEmp/{eno}")
    public String delEmp(@PathVariable Integer eno, Model model) {
        boolean b=empServices.delEmp(eno);
        String msg = "删除失败";
        if (b) {
            msg = "删除成功";
        }
        model.addAttribute("msg", msg);
        return "message";
    }

    @RequestMapping("/login")
    public String login(String ename, String pwd, Model model){
        System.out.println("ename:"+ename+"----pwd:"+pwd);
        if (ename.equals("admin")&&pwd.equals("123")) {
            System.out.println("登录成功");
            Emp emp = new Emp();
            emp.setEname("admin");
            //把数据设置到request作用域中
            model.addAttribute("emp",emp);
            return "index";
        }else{
            //重定向
            return "redirect:/login.jsp";
        }
    }
}
